package com.onehash.onebilling.gui.panel;

import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.io.IOException;

import com.onehash.onebilling.gui.*;
import com.onehash.onebilling.entity.*;
import com.onehash.onebilling.entity.User.*;

public class AddUserUI extends JPanel {

	private UserRegister userRegister;

    JLabel imageLabel = new JLabel();
    JLabel Lheader = new JLabel();
    JLabel lUserName = new JLabel();
    JLabel lPassword = new JLabel();
    JLabel lConfirmPassword = new JLabel();
    JLabel lRole = new JLabel();
    JTextField tfUserName = new JTextField();
    JTextField tfPassword = new JPasswordField();
    JTextField tfConfirmPassword = new JPasswordField();
    JRadioButton rbRole2 = new JRadioButton();
    JRadioButton rbRole1 = new JRadioButton();
    JRadioButton rbRole0 = new JRadioButton();
    JLabel lPlan = new JLabel();
    JButton bAdd = new JButton();
    ButtonGroup planGroup = new ButtonGroup();
    // reference to owner
    private JDialog owner;

    public AddUserUI() {
        try {
            userRegister = UserRegister.getInstance();
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public void setOwner(JDialog owner){
        this.owner=owner;
    }

    private void jbInit() throws Exception {
        this.setLayout(null);
        this.setMinimumSize(new Dimension(800,570));
        this.setPreferredSize(new Dimension(800, 570));
        imageLabel.setMaximumSize(new Dimension(800, 570));
        imageLabel.setMinimumSize(new Dimension(800, 570));
        imageLabel.setPreferredSize(new Dimension(800, 570));
        imageLabel.setBounds(new Rectangle(0, 0, 800, 570));
        Lheader.setFont(new java.awt.Font("Dialog", Font.BOLD, 18));
        Lheader.setForeground(Color.orange);
        Lheader.setText(":: Add User ::");
        Lheader.setBounds(new Rectangle(525, 15, 220, 21));
        lUserName.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        lUserName.setText("USER NAME:");
        lUserName.setBounds(new Rectangle(107, 156, 102, 14));
        lPassword.setText("PASSWORD:");
        lPassword.setBounds(new Rectangle(107, 194, 102, 14));
        lPassword.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        lConfirmPassword.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        lConfirmPassword.setText("CONFIRM PASSWORD:");
        lConfirmPassword.setBounds(new Rectangle(107, 231, 102, 14));
        
        tfUserName.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        tfUserName.setBounds(new Rectangle(240, 156, 120, 18));
        tfPassword.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        tfPassword.setBounds(new Rectangle(240, 194, 137, 18));
        tfConfirmPassword.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        //tfConfirmPassword.setToolTipText("");
        tfConfirmPassword.setBounds(new Rectangle(240, 231, 138, 18));
        
        rbRole2.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        rbRole2.setOpaque(false);
        rbRole2.setText("AGENT");
        rbRole2.setBounds(new Rectangle(240, 370, 237, 19));
        rbRole2.setActionCommand("AGENT");
        rbRole1.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        rbRole1.setOpaque(false);
        rbRole1.setText("ADMIN");
        rbRole1.setActionCommand("ADMIN");
        rbRole1.setBounds(new Rectangle(240, 390, 237, 19));
        rbRole0.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        rbRole0.setOpaque(false);
        rbRole0.setText("SUPERADMIN");
        rbRole0.setActionCommand("SUPERADMIN");
        rbRole0.setBounds(new Rectangle(240, 412, 237, 19));
        
        lPlan.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        lPlan.setText("User Role:");
        lPlan.setBounds(new Rectangle(107, 393, 89, 21));
        rbRole2.setSelected(true);
        
        bAdd.setBounds(new Rectangle(496, 454, 84, 25));
        bAdd.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
        bAdd.setBorder(BorderFactory.createLineBorder(Color.black));
        bAdd.setActionCommand("add");
        bAdd.setText("ADD");
        bAdd.addActionListener(new AddUserUI_bAdd_actionAdapter(this));       
        planGroup.add(rbRole2);
        planGroup.add(rbRole1);
        planGroup.add(rbRole0);
        
        this.add(tfUserName);
        this.add(tfPassword);
        this.add(tfConfirmPassword);
        this.add(rbRole2);
        this.add(rbRole1);
        this.add(rbRole0);
        this.add(Lheader);
        this.add(lUserName);
        this.add(lPassword);
        this.add(lConfirmPassword);
        this.add(lPlan);
        this.add(bAdd);
        this.add(imageLabel);
        imageLabel.setIcon(new ImageIcon(GenericDialog.class.getResource("images/BgTransaction.jpg")));
    }

    /* Add button clicked */
    public void bAdd_actionPerformed(ActionEvent e) {
        /* check for empty fields */
        if( emptyFields() ){
            MessageDialog.showInfoDialog( (Frame)owner.getOwner(), owner.getTitle(), "Please fill in all particular before proceeding!" );
        }
        
        /* check if User name entered is already exist */
        else if( userRegister.getUser(tfUserName.getText())!=null ){
            MessageDialog.showInfoDialog( (Frame)owner.getOwner(), owner.getTitle(), "User Name already Exist!" );
        }
        /* check if password and confirm password entered are same */
        else if( !(tfPassword.getText().equals(tfConfirmPassword.getText()))){
            MessageDialog.showInfoDialog( (Frame)owner.getOwner(), owner.getTitle(), "Password and Confirm Password Do Not Match!" );
        }
        
        else{
            // display last confirmation
            int ans = MessageDialog.showQuestionDialog((Frame)owner.getOwner(), owner.getTitle(), "User: "+tfUserName.getText()+", "+tfPassword.getText()+" will be added. \nContinue?");
            //everything ok. Call controller to add member
            if( ans == MessageDialog.YES_OPTION ) {
            	boolean success = false;
            	
            	try {
            		User user = userRegister.addUser(EnumUserRole.valueOf(planGroup.getSelection().getActionCommand()), tfUserName.getText(), tfPassword.getText().toString());
            		if (user != null) success = true;
            	}catch (IOException ex){
            		success = false;
            	}
                displayInfoDialog(success);
                clearFields();
            }
        }
    }

    /* Method to display whether the new member was successfully added */
    public void displayInfoDialog( boolean success ){
        if(success ){
            MessageDialog.showInfoDialog((Frame)owner.getOwner(), owner.getTitle(), "New User: "+tfUserName.getText()+" Successfully Added!" );
        }
        else {
            MessageDialog.showErrorDialog( (Frame)owner.getOwner(), owner.getTitle(), "User: "+tfUserName.getText()+" Cannot Add!" );
        }
    }

    /* Method to clear the text input fields */
    private void clearFields(){
    	tfUserName.setText("");
        tfPassword.setText("");
        tfConfirmPassword.setText("");
        rbRole2.setSelected(true);
    }

    /* Method to check for empty inputs */
    private boolean emptyFields(){
        if( tfUserName.getText().compareTo("")==0 )
            return true;
        if( tfPassword.getText().compareTo("")==0)
            return true;
        if( tfConfirmPassword.getText().compareTo("")==0 )
            return true;

        return false;
    }
}


class AddUserUI_bAdd_actionAdapter implements ActionListener {
    private AddUserUI adaptee;
    AddUserUI_bAdd_actionAdapter(AddUserUI adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.bAdd_actionPerformed(e);
    }
}
